<?php

namespace Home\Model;

class VolleyDescModel extends CommonModel
{
    public function index($user_id, $money, $remark, $reward = 0)
    {
        $User = new UserModel;
        $Self = new VolleyDescModel;
        $Config = new ConfigSystemModel;
        
        $userInfo = $User->master(true)->where(['id' => $user_id])->getField('id, sex, volley, volley_gift');
        $balance = $userInfo[$user_id]['volley'];
        $balance_gift = $userInfo[$user_id]['volley_gift'];

        $chat_over_dec_not = $Config->where(['title' => 'chat_over_dec_not'])->getField('content');
        $hasnotdec = in_array($user_id, explode(',', $chat_over_dec_not));

        $real_money = changefloor($money);
        $use_gift_money = 0;
        if (!$reward && $balance_gift>0) {
            $use_gift_money = floatval($money>$balance_gift?$balance_gift:$money);
            $remark .= ("(含".$use_gift_money."赠送金币)");
        }

        $current_balance = $reward ? ($balance + $money) : ($balance - $real_money);

        if ($money > 0) {
            $method = $reward ? 'setInc' : 'setDec';

            $User->where(['id' => $user_id])->$method('volley', $real_money);
            if ($use_gift_money>0) {
                $User->where(['id' => $user_id])->$method('volley_gift', $use_gift_money);
            }

            $Self->add([
                'user_id' => $user_id,
                'content' => $remark,
                'price' => $money,
                'types' => $reward,
                'balance' => $current_balance,
                'time' => time()
            ]);

            if (!$reward) {
                // 用户消费统计
                (new UserDataModel)->createData($user_id, $real_money, 'consume');
            }
        }
        return true;
    }


    public function beans($user_id, $money, $remark, $reward = 0)
    {
        $User = new UserModel;
        $Self = new VolleyDescModel;
        
        $userInfo = $User->master(true)->where(['id' => $user_id])->getField('id, sex, beans');
        $balance = $userInfo[$user_id]['beans'];

        $real_money = changefloor($money);
        $current_balance = $reward ? ($balance + $money) : ($balance - $real_money);

        if ($money > 0) {
            $method = $reward ? 'setInc' : 'setDec';

            $User->where(['id' => $user_id])->$method('beans', $real_money);

            $Self->add([
                'user_id' => $user_id,
                'content' => $remark,
                'price' => $money,
                'types' => $reward,
                'balance' => $current_balance,
                'desc' => 2,
                'time' => time()
            ]);
        }
        return true;
    }
}
